{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "引入模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "百度大脑 AI开放平台"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.f0929868484a238f50849ae17b848d82.315360000.1902066634.282335-19388362', 'expires_in': 2592000, 'session_key': '9mzdDcOq9QPIj4xQ3WZwWqebkXO+gmDXM2iRzvvx634FI5L2hGYcXGTEmuLWgcsfeVf67CK9D+aJzpqT8ve7MnXzCRlI7g==', 'access_token': '24.65e8ef2995b776cb997faf5929ef540f.2592000.1589298634.282335-19388362', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi qatest_scope1 fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': '0835888d0640276cea1d86f610e4cb3c'}\n"
     ]
    }
   ],
   "source": [
    "# 获取 access_token\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=5gUsEEOPA8DVCGGpXsEsiv01&client_secret=QF3b9PG1UVxSBPBIxVflqFwnUM5iYz7X'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 8965056525555, 'timestamp': 1586706635, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'd62bad7fc03f34ed469cc4757e8d1683', 'location': {'left': 85.91, 'top': 89.58, 'width': 105, 'height': 100, 'rotation': 2}, 'face_probability': 1, 'angle': {'yaw': 10, 'pitch': 22.34, 'roll': -6.14}, 'age': 22, 'gender': {'type': 'female', 'probability': 1}, 'emotion': {'type': 'neutral', 'probability': 0.65}}]}}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "'''\n",
    "人脸检测与属性分析\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1281705909,1438385339&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,emotion\\\"}\"\n",
    "\n",
    "access_token = '[24.7d46254a6377d74cc3b7e961ffa6d9b6.2592000.1589291369.282335-19388362]'\n",
    "\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "headers = {'content-type': 'application/json'}\n",
    "\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n",
      "{'type': 'female', 'probability': 1}\n",
      "{'type': 'neutral', 'probability': 0.65}\n"
     ]
    }
   ],
   "source": [
    "print(response.json()[\"result\"][\"face_list\"][0][\"age\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"gender\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"emotion\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多人01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 4525354500194, 'timestamp': 1586705940, 'cached': 0, 'result': {'face_num': 2, 'face_list': [{'face_token': '5db9f7ed19f098c9c1c755f6b82dc432', 'location': {'left': 246.93, 'top': 154.91, 'width': 194, 'height': 199, 'rotation': -9}, 'face_probability': 1, 'angle': {'yaw': -6.79, 'pitch': 12.42, 'roll': -9.84}, 'age': 25, 'gender': {'type': 'female', 'probability': 1}, 'emotion': {'type': 'happy', 'probability': 1}}, {'face_token': '1384a295ba29e4fdb73b09f3ee334fde', 'location': {'left': 54.41, 'top': 197.79, 'width': 183, 'height': 159, 'rotation': -24}, 'face_probability': 1, 'angle': {'yaw': -29.07, 'pitch': 17.37, 'roll': -17.68}, 'age': 22, 'gender': {'type': 'female', 'probability': 1}, 'emotion': {'type': 'happy', 'probability': 1}}]}}\n"
     ]
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1089230427,3018585621&fm=11&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,emotion\\\",\\\"max_face_num\\\":\\\"2\\\"}\"\n",
    "\n",
    "access_token = '[24.7d46254a6377d74cc3b7e961ffa6d9b6.2592000.1589291369.282335-19388362]'\n",
    "\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "headers = {'content-type': 'application/json'}\n",
    "\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "{'type': 'female', 'probability': 1}\n",
      "{'type': 'happy', 'probability': 1}\n"
     ]
    }
   ],
   "source": [
    "baidu_api_first_woman_age = data[\"result\"][\"face_list\"][0][\"age\"]\n",
    "baidu_api_first_woman_gender = data[\"result\"][\"face_list\"][0][\"gender\"]\n",
    "baidu_api_first_woman_emotion = data[\"result\"][\"face_list\"][0][\"emotion\"]\n",
    "print(baidu_api_first_woman_age)\n",
    "print(baidu_api_first_woman_gender)\n",
    "print(baidu_api_first_woman_emotion)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n",
      "{'type': 'female', 'probability': 1}\n",
      "{'type': 'happy', 'probability': 1}\n"
     ]
    }
   ],
   "source": [
    "baidu_api_second_woman_age = data[\"result\"][\"face_list\"][1][\"age\"]\n",
    "baidu_api_second_woman_gender = data[\"result\"][\"face_list\"][1][\"gender\"]\n",
    "baidu_api_second_woman_emotion = data[\"result\"][\"face_list\"][1][\"emotion\"]\n",
    "print(baidu_api_second_woman_age)\n",
    "print(baidu_api_second_woman_gender)\n",
    "print(baidu_api_second_woman_emotion)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多人02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 7965841545001, 'timestamp': 1586707282, 'cached': 0, 'result': {'face_num': 2, 'face_list': [{'face_token': '5a6499abf60dd1257ccde2ccfa2e3bd9', 'location': {'left': 416.45, 'top': 185.52, 'width': 240, 'height': 222, 'rotation': 12}, 'face_probability': 1, 'angle': {'yaw': -26.77, 'pitch': 11.98, 'roll': 16.04}, 'age': 23, 'gender': {'type': 'male', 'probability': 1}, 'emotion': {'type': 'neutral', 'probability': 0.91}}, {'face_token': '17a8c0eade5dddf82ed19a95c12ed4bf', 'location': {'left': 665.04, 'top': 404.95, 'width': 221, 'height': 208, 'rotation': -8}, 'face_probability': 1, 'angle': {'yaw': 32.39, 'pitch': 16.82, 'roll': -17.76}, 'age': 22, 'gender': {'type': 'female', 'probability': 1}, 'emotion': {'type': 'happy', 'probability': 1}}]}}\n"
     ]
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586717344803&di=c95d6ae9aa47ea73e8c5eed1c0705474&imgtype=0&src=http%3A%2F%2Fangugulife.com%2Fwp-content%2Fuploads%2F2016%2F10%2F1477576345-5522-a1abc8662b.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,emotion\\\",\\\"max_face_num\\\":\\\"2\\\"}\"\n",
    "\n",
    "access_token = '[24.7d46254a6377d74cc3b7e961ffa6d9b6.2592000.1589291369.282335-19388362]'\n",
    "\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "headers = {'content-type': 'application/json'}\n",
    "\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 7965841545001,\n",
       " 'timestamp': 1586707282,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 2,\n",
       "  'face_list': [{'face_token': '5a6499abf60dd1257ccde2ccfa2e3bd9',\n",
       "    'location': {'left': 416.45,\n",
       "     'top': 185.52,\n",
       "     'width': 240,\n",
       "     'height': 222,\n",
       "     'rotation': 12},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -26.77, 'pitch': 11.98, 'roll': 16.04},\n",
       "    'age': 23,\n",
       "    'gender': {'type': 'male', 'probability': 1},\n",
       "    'emotion': {'type': 'neutral', 'probability': 0.91}},\n",
       "   {'face_token': '17a8c0eade5dddf82ed19a95c12ed4bf',\n",
       "    'location': {'left': 665.04,\n",
       "     'top': 404.95,\n",
       "     'width': 221,\n",
       "     'height': 208,\n",
       "     'rotation': -8},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': 32.39, 'pitch': 16.82, 'roll': -17.76},\n",
       "    'age': 22,\n",
       "    'gender': {'type': 'female', 'probability': 1},\n",
       "    'emotion': {'type': 'happy', 'probability': 1}}]}}"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n",
      "{'type': 'male', 'probability': 1}\n",
      "{'type': 'neutral', 'probability': 0.91}\n"
     ]
    }
   ],
   "source": [
    "print(response.json()[\"result\"][\"face_list\"][0][\"age\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"gender\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"emotion\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n",
      "{'type': 'female', 'probability': 1}\n",
      "{'type': 'happy', 'probability': 1}\n"
     ]
    }
   ],
   "source": [
    "print(response.json()[\"result\"][\"face_list\"][1][\"age\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][1][\"gender\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][1][\"emotion\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
